package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.Article;
import com.itheima.entity.YddTag;
import com.itheima.mapper.IndexMapper;
import com.itheima.service.ConfigService;
import com.itheima.service.IndexService;
import com.itheima.service.TagService;
import com.itheima.service.YddCateService;
import com.itheima.vo.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class IndexServiceImpl implements IndexService {
    private final IndexMapper indexMapper;
    private final YddCateService yddCateService;
    private final ConfigService configService;
    private final TagService tagService;

    /**
     * 文章详情数据
     *
     * @param id
     * @return {@link ArticleShowVO}
     */
    @Override
    public ArticleShowVO show(Long id) {
        ArticleShowVO articleShowVO = new ArticleShowVO();

        //todo 先拿到该文章的所有信息
        Article article = indexMapper.selectById(id);
        //todo 1.上一篇数据
        List<ShowVO> prevList = indexMapper.selectIDAndTitleByID(id);
        if(!Objects.isNull(prevList)){
            articleShowVO.setPrev(prevList);
        }
        //todo 2.下一篇数据
        List<ShowVO> nextList = indexMapper.selectNextIDAndTitleByID(id);
        if (!Objects.isNull(nextList)){
            articleShowVO.setNext(nextList);
        }

        //todo 3.文章的所有数据

        InfoVO infoVO = new InfoVO();
        BeanUtils.copyProperties(article,infoVO);
        infoVO.setCreate_date(article.getCreateDate());
        infoVO.setUpdate_date(article.getUpdateDate());
        infoVO.setUser_id(article.getUserId());
        String catename = indexMapper.selectCatename(article.getCateid());
        infoVO.setCatename(catename);
        infoVO.setIsLike(0);
        infoVO.setIsCollect(0);

        articleShowVO.setInfo(infoVO);

        //todo 4.推荐文章数据
        List<Article> articleList = indexMapper.selectByIshot();
        articleShowVO.setRecommend(articleList);

        return articleShowVO;
    }

    /**
     *文章列表数据
     *
     * @param page
     * @param limit
     * @param cateid
     * @return {@link PageInfoVO}<{@link YddArticleVO2}>
     */
    @Override
    public PageInfoVO<YddArticleVO2> list(Integer page, Integer limit, Integer cateid) {
        //todo 1.开启
        PageHelper.startPage(page,limit);
        //todo 2.原始查询
        List<Article> articleList = indexMapper.selectByCateId(cateid);
        List<YddArticleVO2> yddArticleVO2List = articleList.stream().map(article -> {
            YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
            BeanUtils.copyProperties(article, yddArticleVO2);
            yddArticleVO2.setCreate_date(article.getCreateDate());
            yddArticleVO2.setUpdate_date(article.getUpdateDate());
            String catename = indexMapper.selectCatename(article.getCateid());
            yddArticleVO2.setCatename(catename);
            return yddArticleVO2;
        }).collect(Collectors.toList());
        //todo 3.数据转换
        PageInfo<YddArticleVO2> pageInfo = PageInfo.of(yddArticleVO2List);

        //todo 4.封装结果返回
        PageInfoVO<YddArticleVO2> list = new PageInfoVO<>();
        list.setCount(pageInfo.getTotal());
        list.setPageSize(limit);
        list.setCurrentPage(pageInfo.getPageNum());
        list.setTotalPages(pageInfo.getPages());
        list.setData(pageInfo.getList());
        return list;
    }

    /**
     * 推荐文章---ishot = 1,istop = 1
     *
     * @return {@link RecommendVO}
     */
    @Override
    public RecommendVO recommend() {
        RecommendVO recommendVO = new RecommendVO();
        recommendVO.setDesc("侧边推荐文章");
        List<Article> articleList = indexMapper.selectRecommend();
        List<YddArticleVO2> yddArticleVO2List = articleList.stream().map(new Function<Article, YddArticleVO2>() {
            @Override
            public YddArticleVO2 apply(Article article) {
                YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
                BeanUtils.copyProperties(article, yddArticleVO2);
                String catename = indexMapper.selectCatename(article.getCateid());
                yddArticleVO2.setCatename(catename);
                return yddArticleVO2;
            }
        }).collect(Collectors.toList());
        recommendVO.setList(yddArticleVO2List);
        return recommendVO;
    }

    /**
     * 最新文章
     *
     * @return
     */
    @Override
    public RecommendVO hot() {
        RecommendVO hotVO = new RecommendVO();
        hotVO.setDesc("侧边推荐文章");
        List<Article> articleList = indexMapper.selectHot();
        List<YddArticleVO2> yddArticleVO2List = articleList.stream().map(new Function<Article, YddArticleVO2>() {
            @Override
            public YddArticleVO2 apply(Article article) {
                YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
                BeanUtils.copyProperties(article, yddArticleVO2);
                String catename = indexMapper.selectCatename(article.getCateid());
                yddArticleVO2.setCatename(catename);
                return yddArticleVO2;
            }
        }).collect(Collectors.toList());
        hotVO.setList(yddArticleVO2List);
        return hotVO;
    }

    /**
     * 热门置顶文章数据(首页)
     *
     * @param page
     * @param limit
     * @return {@link HotAndTopVO}
     */
    @Override
    public HotAndTopVO hotAndTop(Integer page, Integer limit) {
        HotAndTopVO hotAndTopVO = new HotAndTopVO();
        hotAndTopVO.setDesc("首页大列表");
        // 获取当前时间的时间戳（毫秒数）
        long timestamp = System.currentTimeMillis();
        // 计算24小时前的时间戳（毫秒数）
        long previousTimestamp = timestamp - 24 * 60 * 60 * 1000;
        Integer num_day = indexMapper.selectCountByCreateDate(previousTimestamp,timestamp);
        hotAndTopVO.setNum_day(num_day);

        long previousWeekTimestamp = timestamp - 7*24 * 60 * 60 * 1000;
        Integer num_week = indexMapper.selectCountByCreateDate(previousWeekTimestamp,timestamp);
        hotAndTopVO.setNum_week(num_week);

        //todo 分页
        //todo 1.开启
        PageHelper.startPage(page,limit);
        //todo 2.原始查询
        List<Article> articleList = indexMapper.selectByHotAndTop();
        List<YddArticleVO2> yddArticleVO2List = articleList.stream().map(article -> {
            YddArticleVO2 yddArticleVO2 = new YddArticleVO2();
            BeanUtils.copyProperties(article, yddArticleVO2);
            yddArticleVO2.setCreate_date(article.getCreateDate());
            yddArticleVO2.setUpdate_date(article.getUpdateDate());
            String catename = indexMapper.selectCatename(article.getCateid());
            yddArticleVO2.setCatename(catename);
            return yddArticleVO2;
        }).collect(Collectors.toList());
        //todo 3.数据转换
        PageInfo<YddArticleVO2> pageInfo = PageInfo.of(yddArticleVO2List);

        //todo 4.封装结果返回
        PageInfoVO<YddArticleVO2> list = new PageInfoVO<>();
        list.setCount(pageInfo.getTotal());
        list.setPageSize(limit);
        list.setCurrentPage(pageInfo.getPageNum());
        list.setTotalPages(pageInfo.getPages());
        list.setData(pageInfo.getList());

        hotAndTopVO.setList(list);

        return hotAndTopVO;
    }


    /**
     * 首页和全局数据
     *
     * @return {@link IndexVO}
     */
    @Override
    public IndexVO index() {
        IndexVO indexVO = new IndexVO();
        //todo 1.所有分类
        List<CateVO2> allCate = yddCateService.allData();
        indexVO.setAllCate(allCate);

        //todo 2.所有配置数据
        IndexConfigVO indexConfigVO = new IndexConfigVO();
        ConfigAllVO configAllVO = configService.all();
        indexConfigVO.setList(configAllVO.getList());
        indexConfigVO.setAllData(configAllVO.getAllData());
        indexVO.setConfig(indexConfigVO);

        //todo 3.所有标签
        List<YddTag> allTag = tagService.all();
        indexVO.setAllTag(allTag);

        //todo 4.所有友情链接（文字和图片）
        List<LinkVO> allLink = indexMapper.selectAllLink();
        indexVO.setAllLink(allLink);

        //todo 5.轮播图数据
        Integer bannerId = 15;
        List<BannerVO> banner = indexMapper.selectBannerById(bannerId);
        indexVO.setBanner(banner);

        //todo 6.侧边栏图片
        Integer sideBannerId = 14;
        List<BannerVO> sideBanner = indexMapper.selectBannerById(sideBannerId);
        indexVO.setSideBanner(sideBanner);

        //todo 7.详情广告图
        List<Integer> showBannerIds = new ArrayList<>();
        showBannerIds.add(12);
        showBannerIds.add(13);
        List<BannerVO> showBanner = indexMapper.selectShowBannerById(showBannerIds);
        indexVO.setShowBanner(showBanner);

        return indexVO;
    }


}
